package com.cc.user.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.common.utils.*;
import com.cc.user.entity.ScheduleEntity;
import com.cc.user.entity.UserEntity;
import com.cc.user.feign.MallFeign;
import com.cc.user.service.ScheduleService;
import com.cc.user.vo.DemandVo;
import com.cc.user.vo.DueVo;
import com.cc.user.vo.OfflineOrderEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.user.entity.DemandnoteEntity;
import com.cc.user.service.DemandnoteService;
import com.cc.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 11:30:58
 */
@RestController
@RequestMapping("user/demandnote")
@Api(tags="缴款")
public class DemandnoteController {

    @Autowired
    private MallFeign mallFeign;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private DemandnoteService demandnoteService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据缴款id获取缴款")
    public DemandnoteEntity info(@PathVariable("id") Long id){
		DemandnoteEntity demandnote = demandnoteService.getById(id);
        return demandnote;
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存缴款")
    public ResultMsg save(@RequestBody DemandnoteEntity demandnote){
        ResultMsg resultMsg = new ResultMsg();
		demandnoteService.save(demandnote);
        resultMsg.setMsg("保存成功");
        return resultMsg;
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新缴款")
    public ResultMsg update(@RequestBody DemandnoteEntity demandnote){
        ResultMsg resultMsg = new ResultMsg();
		demandnoteService.updateById(demandnote);
        resultMsg.setMsg("更新成功");
        return resultMsg;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("根据id List删除缴款")
    public ResultMsg delete(@RequestBody Long[] ids){
        ResultMsg resultMsg = new ResultMsg();
		demandnoteService.removeByIds(Arrays.asList(ids));
        resultMsg.setMsg("删除成功");
        return resultMsg;
    }

    @ApiOperation("获取所有应缴款项")
    @GetMapping("/getAllYJKById")
    public ResponseData<List<DueVo>> getAllBillById(HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        List<DueVo> dueVo = demandnoteService.getAllYJKById(uid);
        return ResponseData.success(dueVo);
    }

    @ApiOperation("查询所有缴款记录")
    @GetMapping("/getAllDemandnote")
    public ResponseData<List<DemandnoteEntity>> getAllDemandnote(HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        List<DemandnoteEntity> list = demandnoteService.getAllDemandnote(uid);
        return ResponseData.success(list);
    }

    @ApiOperation("去缴款")
    @PostMapping("/toJK")
    public ResponseData toJK(@ApiParam("线下订单id") @RequestParam("ooid") Long ooid,
                  @ApiParam("收款人手机号") @RequestParam("phone") String phone,
                  @ApiParam("收款金额") @RequestParam("receiveMoney") String receiveMoney,
                  @ApiParam("收款凭证") @RequestBody List<String> pics,
                             HttpSession session){
        Long jkId = demandnoteService.toJK(ooid, phone, receiveMoney, pics);
        // 添加待办事项
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setUid(uid);
        scheduleEntity.setCreatetime(new Date());
        scheduleEntity.setScheduleStatus(0);
        scheduleEntity.setContent("您的缴款【"+jkId+"】【待审核】");
        scheduleService.save(scheduleEntity);
        return ResponseData.success("缴款提交成功",true);
    }

    @ApiOperation("后台获取所有的缴款记录")
    @GetMapping("/getAllDemand")
    public Page<DemandVo> getAllDemand(@RequestParam(required = false) Map<String,Object> params) throws ParseException {
        Page<DemandVo> page =demandnoteService.getAllDemand(params);
        return page;
    }

    // todo 改变线下订单的已付款
    @ApiOperation("审核线下缴款")
    @GetMapping("/shenhe")
    public ResultMsg shenhe(@RequestParam("id") Long id,
                    @RequestParam("ooid") Long ooid,
                            @RequestParam("type") Integer type){
        ResultMsg resultMsg = new ResultMsg();
        demandnoteService.shenhe(id,ooid,type);
        resultMsg.setMsg("成功");
        // 添加待办事项
        R r = mallFeign.info(ooid);
        OfflineOrderEntity offlineOrder = r.getData("offlineOrder", new TypeReference<OfflineOrderEntity>() {
        });
        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setUid(offlineOrder.getCid());
        scheduleEntity.setCreatetime(new Date());
        scheduleEntity.setScheduleStatus(0);
        if(type == 1){
            scheduleEntity.setContent("您的缴款【"+id+"】【审核通过】");
        }else {
            scheduleEntity.setContent("您的缴款【"+id+"】【审核不通过】");
        }
        scheduleService.save(scheduleEntity);
        return resultMsg;
    }

    @ApiOperation("获取应缴款详情")
    @GetMapping("/getYJKXQ")
    public ResponseData<DueVo> getYJKXQ(@ApiParam("线下订单id") @RequestParam("id") Long id){
        DueVo dueVo = demandnoteService.getYJKXQ(id);
        return ResponseData.success(dueVo);
    }

}
